package org.numb.concurrency.chapter03;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

public class OptimisticLock {

    public static void main(String[] args) {
//        noLockScene();
//        synchronizedScene();
//        reentrantLockScene();
        optimisticLockScene();
    }


    private static Integer num = 0;

    public static void noLockScene() {

        Thread t1 = new Thread(() -> {
            while (true) {
                if (num % 2 == 1) {
                    try {
                        TimeUnit.MICROSECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("thread 1, num = " + num);
                }
            }
        });

        Thread t2 = new Thread(() -> {
            while (true) {
                try {
                    TimeUnit.MICROSECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                num++;
            }

        });
        t1.start();
        t2.start();
    }

    private static byte[] lock = new byte[0];

    public static void synchronizedScene() {
        Thread t1 = new Thread(() -> {
            while (true) {
                synchronized (lock) {
                    if (num % 2 == 1) {
                        try {
                            TimeUnit.MICROSECONDS.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("thread 1, num = " + num);
                    }
                }
            }

        });

        Thread t2 = new Thread(() -> {
            while (true) {
                synchronized (lock) {
                    try {
                        TimeUnit.MICROSECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    num++;
                }
            }
        });
        t1.start();
        t2.start();
    }

    public static void reentrantLockScene() {
        ReentrantLock reentrantLock = new ReentrantLock();
        Thread t1 = new Thread(() -> {
            while (true) {
                reentrantLock.lock();
                if (num % 2 == 1) {
                    try {
                        TimeUnit.MICROSECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("thread 1, num = " + num);
                }
                reentrantLock.unlock();
            }
        });

        Thread t2 = new Thread(() -> {
            while (true) {
                reentrantLock.lock();
                try {
                    TimeUnit.MICROSECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                num++;
                reentrantLock.unlock();
            }
        });
        t1.start();
        t2.start();
    }

    public static void optimisticLockScene() {
        AtomicInteger number = new AtomicInteger();
        Thread t1 = new Thread(() -> {
            while (true) {
                int origin = number.get();
                if (origin % 2 == 1) {
                    try {
                        TimeUnit.MICROSECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (!number.compareAndSet(origin, origin)) {
                        System.out.println("the num has changed");
                    } else {
                        System.out.println("thread 1, num = " + origin);
                    }
                }
            }
        });

        Thread t2 = new Thread(() -> {
            while (true) {
                try {
                    TimeUnit.MICROSECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                number.incrementAndGet();
            }
        });
        t1.start();
        t2.start();
    }
}
